{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.neighbors import KNeighborsRegressor\n",
    "\n",
    "X_train = np.array([\n",
    "    [158, 64, 1],\n",
    "    [170, 86, 1],\n",
    "    [183, 84, 1],\n",
    "    [191, 80, 1],\n",
    "    [155, 49, 0],\n",
    "    [163, 59, 0],\n",
    "    [180, 67, 0],\n",
    "    [158, 54, 0],\n",
    "    [170, 67, 0]\n",
    "])\n",
    "y_train = [7, 12, 29, 18, 11, 16, 29, 22, 36]\n",
    "\n",
    "X_test = np.array([\n",
    "    [160, 66, 1],\n",
    "    [196, 87, 1],\n",
    "    [168, 68, 0],\n",
    "    [177, 74, 0]\n",
    "])\n",
    "y_test = [9, 13, 26, 21]\n",
    "\n",
    "K = 1\n",
    "clf = KNeighborsRegressor(n_neighbors=K)\n",
    "clf.fit(X_train, y_train)\n",
    "predictions = clf.predict(np.array(X_test))\n",
    "predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  7.,  18.,  36.,  29.])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-0.0919377652051\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import r2_score\n",
    "print(r2_score(y_test, predictions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# todo, instead, weight from height and gender"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Predicted weights: [ 59.          77.          70.66666667  72.66666667]\n",
      "Actual weights: [66, 87, 68, 74]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.neighbors import KNeighborsRegressor\n",
    "\n",
    "X_train = np.array([\n",
    "    [158, 1],\n",
    "    [170, 1],\n",
    "    [183, 1],\n",
    "    [191, 1],\n",
    "    [155, 0],\n",
    "    [163, 0],\n",
    "    [180, 0],\n",
    "    [158, 0],\n",
    "    [170, 0]\n",
    "])\n",
    "y_train = [64, 86, 84, 80, 49, 59, 67, 54, 67]\n",
    "\n",
    "X_test = np.array([\n",
    "    [160, 1],\n",
    "    [196, 1],\n",
    "    [168, 0],\n",
    "    [177, 0]\n",
    "])\n",
    "y_test = [66, 87, 68, 74]\n",
    "\n",
    "K = 3\n",
    "clf = KNeighborsRegressor(n_neighbors=K)\n",
    "clf.fit(X_train, y_train)\n",
    "predictions = clf.predict(np.array(X_test))\n",
    "print('Predicted weights: %s' % predictions)\n",
    "print('Actual weights: %s' % y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.616744186047\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import r2_score\n",
    "print(r2_score(y_test, predictions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
